
package com.showcai.controller.schedule;

import com.showcai.controller.base.BaseController;
import com.showcai.module.Schedule;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ScheduleService;
import com.showcai.service.UserService;
import com.showcai.tools.PushTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/schedule")
@Controller
public class ScheduleController extends BaseController {
    @Autowired
    ScheduleService scheduleService;
    @Autowired
    UserService userService;

    /**
     * 获取全部日程列表
     *
     * @param devicId 设备Id
     * @return
     */
    @RequestMapping(value = "/get/scheduleList/{devicId}", method = RequestMethod.POST)
    @ResponseBody
    public String getMediaList(@PathVariable String devicId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setData(scheduleService.getScheduleListByDevicId(devicId));
        logger.info(resultObj.toJson());

        return resultObj.toJson();
    }

    /**
     * 修改日程列表
     *
     * @param schedule 日程
     * @return
     */
    @RequestMapping(value = "/modifySchedule/{devicId}/{userId}", method = RequestMethod.POST)
    @ResponseBody
    public String modifySchedule(Schedule schedule, @PathVariable String devicId, @PathVariable String userId) {
        System.out.println(request.getParameterMap().toString());
        super.showRequestParameters();
        logger.info("modifySchedule=============begin============>>>");
        logger.info(schedule.getTitle());
        ResultObj resultObj = new ResultObj();
        User user = userService.getUserByThirdId(userId);
        if (user == null) {
            throw new MyRunTimeExcption(ReturnCode.USER_IS_INEXISTENCE);
        }
        schedule.setUserId(user.getTid());
        scheduleService.modifySchedule(schedule);
        String body = getPostJson(devicId);
        PushTools.push2DevicService(devicId, body);
        return resultObj.toJson();
    }

    private String getPostJson(String devicId) {

        String url = "https://api.showcai.com";
        // String test_url="http://apitest.showcai.com";
        // return "{\"type\":1,\"devicId\":\""+devicId+"\",\"url\":\""+test_url+"/api/sync/get/scheduleList/"+devicId+"\"}";
        return "{" +
                "    \"type\": 1," +
                "    \"devicId\": \"" + devicId + "\"," +
                "    \"url\": \"" + url + "/api/sync/get/scheduleList/" + devicId + "\"" +
                "}";
    }

    /**
     * 删除日程
     *
     * @param devicId
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{devicId}/{id}", method = RequestMethod.POST)
    @ResponseBody
    public String delSchedule(@PathVariable String devicId, @PathVariable Long id) {
        System.out.println(request.getParameterMap().toString());
        super.showRequestParameters();
        ResultObj resultObj = new ResultObj();
        scheduleService.delScheduleById(devicId, id);
        String body = getPostJson(devicId);
        PushTools.push2DevicService(devicId, body);
        return resultObj.toJson();
    }


}
